/**
 * Created by IntelliJ IDEA.
 * User: dinesh
 * Date: Mar 21, 2012
 * Time: 9:32:44 PM
 * To change this template use File | Settings | File Templates.
 */
/**
	 * Search Operation.
	 */
	function search() {

		// Reset Page Settings (we have to start from page 1)
		//resetPageFields();

		// serialize search criteria
		var params = serializeForm("frmSearch");

		// perform ajax call to search
		ajaxPost(action, "search", params, function(data) {

			var response = toObject(data);//.response;
			/*
			if (response.status==false) {
				// Failure
				handleFailure(response);
				return;
			}*/

			// Search Success
			actionResponse = response;

			// Update Page
		//	updatePage();

			initialize();
		});
	}
/**
	 * Add Operation.
	 */
	function add() {

		initialize(function(){

				enableForm("frmData");

				// Reset  ID
				setField("id", "");

				// Check Status
				setChecked("hotelExist", true);
				setFieldValue("hotelExist", "1");

				isAdd = true;
				setDefaultFocus();
				//setPgEdited(true);
				triggerEvent("add");

			}
     	);
	}

	/**
	 * Edit Operation.
	 */
	function edit() {
		enableForm("frmData");
		setDefaultFocus();
		triggerEvent("edit");
	}

	/**
	 * Save Operation.
	 */
	function save() {

		// data form validation
		if (!validateForm("frmData")) {
			showMessage("W", getMessage("validation-failed"));
			return false;
		}

		// Check for changes
		if (!isPageEdited()) {
			showMessage("W", getMessage("nochanges"));
			return false;
		}

		// concatenate search criteria with form data
		var params = serializeForm("frmData") + "&" + serializeForm("frmSearch");

		ajaxPost(action, "save", params, function(data) {

			var response = toObject(data).response;

			if (response.status==false) {
				// Failure
				handleFailure(response);
				return;
			}

			// Success
			actionResponse = response;

			// Reset page edited flag
			setPgEdited(false);

			// Update Page
			updatePage();

			initialize();

			// Save Success
			if (isAdd) {
				showMessage("C", getMessage("save"));
				isAdd = false;
			}
			else {
				showMessage("C", getMessage("update"));
			}

			triggerEvent("save");
		});
	}

	/**
	 * Delete Operation.
	 */
	function del() {

		getDeleteConfirmation(function() {

			// ------- This will be invoked if user clicks OK.

			// concatenate search criteria with form data
			var params = serializeField("id") + "&" + serializeField("version") + "&" + serializeForm("frmSearch");

			ajaxPost(action, "delete", params, function(data) {

				var response = toObject(data).response;

				if (response.status==false) {
					// Failure
					handleFailure(response);
					return;
				}

				// Success
				actionResponse = response;

				// Update Page
				updatePage();

				initialize();

				// Delete Success
				showMessage("C", getMessage("delete"));
				triggerEvent("delete");
			});

		});

	}


	/* ------- Event Handlers ------ */

	/**
	 * Grid Row Click Handler.
	 *
	 * @param intRow row number which was clicked
	 */
	function onGridRowClick(intRow) {

		if (blnProceedClick) {
			blnProceedClick = false;
			return true;
		}

		// If pending changes, get confirmation
	//	loadCheck(function() {

			// Current selected row clicked again : do nothing
			if (intLastLoaded == intRow) return false;

			// Remove existing messages
			initializeMessage();

			// Clear the data controls
			clearForm("frmData");

			// Disable Form
			disableForm("frmData");

			// Fill data controls
			fillData(intRow);

			// Update last loaded flag
			intLastLoaded = intRow;

			triggerEvent("gridclick");

			blnProceedClick =  true;
			objSG.selectRow(intRow);
			return true;
	//	});

		enforcePrivileges();


	}
/* ------- Page Support Functions ------ */

	function initialize(fnAfterInit) {

		//loadCheck(function() {

			// Clear Message
			//initializeMessage();

			// Clear Data Form
			clearForm("frmData");

			// Disable Form
			disableForm("frmData");

			// Refresh Grid
			//refreshGrid();

			// Reset State
			intLastLoaded = null;

			// Reset Page Edit Flag
			//setPgEdited(false);

		//	setTimeout("setFocus('searchCode')", 300);
			triggerEvent("init");

			if (fnAfterInit) {
				fnAfterInit();
			}
		//});
	}
/**
	 * Fills data into data form.
	 * @param intRow
	 * @return
	 */
	function fillData(intRow) {
		// Automatic form filling (matching JSON fields with field names)
		//autofill("frmData", actionResponse.searchResults.pageData[intRow]);

		// Reset Page Edit Flag
		//setPgEdited(false);
	}

	/**
	 * This function is called when Ajax Search Response is received, to update the
	 * page settings variables.
	 */
	function updatePage() {
//$('#res').html(actionResponse.searchResults.pageData[0].code+'okkkkkkkkkk');
//document.write(actionResponse.searchResults.pageData[code]);
      /*
		// Update grid details
		objSG.seqStartNo = actionResponse.searchResults.startPosition + 1; // Zero based
		objSG.pgNumRecTotal = actionResponse.searchResults.totalRecords;
		objSG.pgNumRecPage = actionResponse.searchResults.pageSize;

		// Assign data to form fields
		setField("totalRecords",  actionResponse.searchResults.totalRecords);
		setField("startPosition", actionResponse.searchResults.startPosition);
		setField("pageSize", actionResponse.searchResults.pageSize);
		*/

	}



	/**
	 * Resets page settings to initial values.
	 */
	function resetPageFields() {
		/*
		setField("totalRecords", 0);
		setField("startPosition", 0);
		setField("pageSize", intRecPerPage);
		*/
	}

	/**
	 * Refreshes the grid and updates data.
	 * @return
	 */
	function refreshGrid() {
		objSG.jsonObject = actionResponse.searchResults;
		objSG.refresh();
	}

	/**
	 * Handles buttons in different events
	 *
	 * @param strEvent name of event
	 */
	function handleButtons(strEvent) {

		// You can write special button handlers here.
		// See commonSystemAeris -> handleDefaultButtons(strEvent).

		// If you don't have special buttons, you may remove this method
		// and corresponding entry from onLoad function's attachEventHandler call.

		// Use disableElement(id) / enableElement(id) to enable disable elements.

		// enableElement will check for 'privileges' attribute and enable only
		// if necessary privileges are present.

		// Sample event handler :
		/*
		switch(strEvent) {
			case "custom_event" :
				// enable / disable buttons
				break;
		}
		*/
	}



	/* ------- Page Initialization ------- */

	/**
	 * Page Load Handler.
	 * <p>
	 * Invoked when the page is loaded.
	 */
	function onLoad(){

		attachDefaultHandlers();

		// Button Handlers
		attachEventHandler(["add", "edit", "delete","save", "load", "init", "gridclick"],
				function(evt) {

					// Default Handler
					handleDefaultButtons(evt.type);

					// Custom Handler
					handleButtons(evt.type);
				}
		);

		triggerEvent("load");

		initialize();

		// Update the page settings
		resetPageFields();

		enforcePrivileges();

		hideProgress();

		search();
	}

	onLoad();
//alert("working")  ;


	/* ----------------------------------------- end of File ----------------------------------- */



